class_name PlayerNormalAttackState
extends GameplayState

@onready var player: Player = $"../../../"

@onready var weapon: Weapon = %Sword

var attack_counter: int = 0

var combo_time: float = 2.0

var combo_max: int = 2

var last_attack_time: float = 0.0


func _on_enter() -> void:
	weapon.animator.visible = true

	player.velocity = Vector2.ZERO
	
	var current_time: float = Time.get_ticks_msec() / 1000.0
	if current_time - last_attack_time > combo_time:
		attack_counter = 0
	else:
		attack_counter = (attack_counter + 1) % combo_max
	
	last_attack_time = current_time

	player.animator.play("attack_" + weapon.weapon_stats.weapon_name + str(attack_counter + 1))
	weapon.animator.play("attack" + str(attack_counter + 1))

	player.player_tag_manager.add_tag(player.player_tag_manager.State_Ground_Attack_Normal)


func _on_update(_delta: float) -> void:
	if not player.animator.is_playing():
		change_state("GhostState", func(): pass)


func _on_exit() -> void:
	weapon.animator.visible = false

	weapon.animator.stop()

	player.player_tag_manager.remove_tag(player.player_tag_manager.State_Ground_Attack_Normal)
